package org.example.controller.tester;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.TokenStorage;
import org.example.entity.task.Scan;
import org.example.service.impl.ScanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/13
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/tester/scan")
public class scanController {
    @Autowired
    private ScanServiceImpl scanService;

    @PostMapping("create")
    public ApiResponse insertScan(HttpSession session, Scan scanVO) {
        try {
            scanVO.setTester((Integer) session.getAttribute(TokenStorage.getToken()));
            if (scanService.insertScan(scanVO)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @GetMapping("list")
    public ApiResponse getScanList(HttpSession session) {
        try {
            return ApiResponse.ok("查询成功",
                    scanService.getScanList((Integer) session.getAttribute(TokenStorage.getToken())));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("查询失败");
        }
    }

    @GetMapping("vul/list")
    public ApiResponse getVulList(HttpSession session) {
        try {
            return ApiResponse.ok("查询成功",
                    scanService.getVulnerabilityList((Integer) session.getAttribute(TokenStorage.getToken())));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("查询失败");
        }
    }
}
